﻿using Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class History : System.Web.UI.Page
{
    
    string aa = @"{ 'status':'0', 'msg':'ok',"
            +" 'result':{ 'caipiaoid':'11', 'issueno':'2016009', 'number':'10 14 24 25 27 32', 'refernumber':'04', 'opendate':'2016-01-21', "
            +" 'deadline':'2016-03-21', 'saleamount':'0',"
            +" 'prize':["
            +" {'prizename':'一等奖','require':'中6+1','num':'5','singlebonus':'10000000'},"
            +" {'prizename':'二等奖','require':'中6+0','num':'140','singlebonus':'271705'},"
            +" {'prizename':'三等奖','require':'中5+1','num':'1178','singlebonus':'3000'},"
            +" {'prizename':'四等奖','require':'中5+0/4+1','num':'65083','singlebonus':'200'},"
            +" {'prizename':'五等奖','require':'中4+0/3+1','num':'1283617','singlebonus':'10'},"
            +" {'prizename':'六等奖','require':'中2+1/1+1/0+1','num':'7931303','singlebonus':'5'}"
            +"]"
            +"}"
            +"}";

    protected void Page_Load(object sender, EventArgs e)
    {
        //LotteryAnnouncement ModelLottery = new LotteryAnnouncement();



        //ModelLottery = fnJSONToObject<LotteryAnnouncement>(aa);
        //IList<Prize> sear = new  List<Prize>();


        //string bb = @"{ 'aaa':'bbbb','rules':[{'field':'xxxx','data':'ooooo','op':'lt'},{'field':'xxxx','data':'ooooo','op':'gt'},{'field':'xxyxx','data':'ooooo','op':'lte'}]}";

        string url = "http://api.jisuapi.com/caipiao/query";
        //string zz = string.Empty;
        for (int j = 2015100; j <= 2015150; j++)
        {
            string param = "appkey=ea357f51462a4a27&caipiaoid=11&issueno="+j+"";

            string result = request(url, param).ToString().ToString();

            long a = GetSearchList(result);
        }



        //JObject jo = (JObject)JsonConvert.DeserializeObject(aa);
        //string zone = jo["result"]["caipiaoid"].ToString();

        //History his = new History();
        //his = fnJSONToObject<History>(aa);
    }

    /// <summary> 
    /// JSON文本转对象,泛型方法 
    /// </summary> 
    /// <typeparam name="T">类型</typeparam> 
    /// <param name="_sJsonText">JSON文本</param> 
    /// <returns>指定类型的对象</returns> 
    static public T fnJSONToObject<T>(string _sJsonText)
    {
        JavaScriptSerializer oJavaSeria = new JavaScriptSerializer();
        try
        {
            return oJavaSeria.Deserialize<T>(_sJsonText);
        }
        catch (Exception ex)
        {
            throw new Exception("JSONHelper.JSONToObject(): " + ex.Message);
        }
    }

    /// <summary>
    /// 发送HTTP请求
    /// </summary>
    /// <param name="url">请求的URL</param>
    /// <param name="param">请求的参数</param>
    /// <returns>请求结果</returns>
    public static string request(string url, string param)
    {
        string strURL = url + '?' + param;
        System.Net.HttpWebRequest request;
        request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
        request.Method = "GET";
        // 添加header
        //request.Headers.Add("apikey", "ea357f51462a4a27");
        System.Net.HttpWebResponse response;
        response = (System.Net.HttpWebResponse)request.GetResponse();
        System.IO.Stream s;
        s = response.GetResponseStream();
        string StrDate = "";
        string strValue = "";
        StreamReader Reader = new StreamReader(s, Encoding.UTF8);
        while ((StrDate = Reader.ReadLine()) != null)
        {
            strValue += StrDate + "\r\n";
        }
        return strValue;
    }

    public long GetSearchList(string jsons)
    {

        // get JSON result objects into a list
        JObject o = (JObject)JsonConvert.DeserializeObject(jsons);

        LotteryAnnouncementZL lottery = new LotteryAnnouncementZL();

        JToken torrentsArray = (JToken)o["result"];
        
        // serialize JSON results into .NET objects
        //IList<Prize> searchResults = new List<Prize>();
        //Prize c = null;

        if (Convert.ToInt32(o["result"]["caipiaoid"]) == 11)
        {
            lottery.LotteryType = 1;
        }
        lottery.LotteryQS = o["result"]["issueno"].ToString();
        lottery.LotteryNo = o["result"]["number"].ToString() + "+" + o["result"]["refernumber"].ToString();
        //DateTime.Parse("2012-2-22").ToString("yyyy-MM-dd")
        //lottery.openTime = Convert.ToDateTime(o["result"]["opendate"]);
        lottery.openTime = Convert.ToDateTime(Convert.ToDateTime(o["result"]["opendate"]).ToString("yyyy-MM-dd"));
        lottery.deadline = Convert.ToDateTime(Convert.ToDateTime(o["result"]["deadline"]).ToString("yyyy-MM-dd"));

        lottery.oneMoney = Convert.ToDecimal(o["result"]["prize"][0]["singlebonus"]);
        lottery.oneCount = Convert.ToInt32(o["result"]["prize"][0]["num"]);

        lottery.twoMoney = Convert.ToDecimal(o["result"]["prize"][1]["singlebonus"]);
        lottery.twoCount = Convert.ToInt32(o["result"]["prize"][1]["num"]);

        lottery.threeMoney = Convert.ToDecimal(o["result"]["prize"][2]["singlebonus"]);
        lottery.threeCount = Convert.ToInt32(o["result"]["prize"][2]["num"]);

        lottery.fourMoney = Convert.ToDecimal(o["result"]["prize"][3]["singlebonus"]);
        lottery.fourCount = Convert.ToInt32(o["result"]["prize"][3]["num"]);

        lottery.fiveMoney = Convert.ToDecimal(o["result"]["prize"][4]["singlebonus"]);
        lottery.fiveCount = Convert.ToInt32(o["result"]["prize"][4]["num"]);

        lottery.sixMoney = Convert.ToDecimal(o["result"]["prize"][5]["singlebonus"]);
        lottery.sixCount = Convert.ToInt32(o["result"]["prize"][5]["num"]);

        lottery.AddTime =Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); 

        long sussue = BLL.LotteryAnnouncementZL.Add(lottery);



        //+" 'result':{ 'caipiaoid':'11', 'issueno':'2016009', 'number':'10 14 24 25 27 32', 'refernumber':'04', 'opendate':'2016-01-21', "
        //   + " 'deadline':'2016-03-21', 'saleamount':'0',"
        //+" {'prizename':'二等奖','require':'中6+0','num':'140','singlebonus':'271705'},"
        //foreach (JToken result in torrentsArray)
        //{
        //    c = new Prize();
        //    //c.prizename = result.SelectToken("prizename").ToString().Replace("\"", "");
        //    //c.singlebonus = result.SelectToken("singlebonus").ToString().Replace("\"", "");
        //    c.num = result.SelectToken("num").ToString().Replace("\"", "");
        //    //c.prize = result.SelectToken("prize").ToString().Replace("\"", "");
        //    //Prize searchResult = JsonConvert.DeserializeObject<Prize>(result.ToString());
        //    searchResults.Add(c);
        //}

        return sussue;
    }

}

